package com.globallogic.vacationplanner.web.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.apache.log4j.Logger;

import com.globallogic.vacationplanner.transfer.dto.impl.VacationDto;
import com.globallogic.vacationplanner.transfer.service.IVacationService;
import com.globallogic.vacationplanner.web.helper.VacationBeanHelper;

@ManagedBean
@SessionScoped
public class VacationBean extends BaseBean implements Serializable {
	private static final long serialVersionUID = 1L;

	private static Logger LOGGER = Logger.getLogger(VacationBean.class);
	private static String I_AM_TIRED_MESSAGE = "Get new Vacation";
	private static String DAY_HAS_BEEN_PAST = "You can not take vacation on the past date";
	private String iAmTired = null;
	private String pastDayEx = null;
	private VacationDto vacationDto = new VacationDto();
	private IVacationService vacationService;
	private List<VacationDto> vacationList = null;
	private int wonderIndex;

	public boolean isDayPast() {
		LOGGER.info("start compare start and end dates");
		long lStartDateTime = getStartDate().getTime();
		long lEndDateTime = getEndDate().getTime();
		long ICurrentDate = new Date().getTime();
		if (lStartDateTime < ICurrentDate || lEndDateTime < ICurrentDate) {
			LOGGER.info("day allready past");
			return true;
		}
		LOGGER.info(" wellcome back to future");
		return false;
	}

	public String getNewVacation() {
		if (isDayPast() == true) {
			pastDayEx = DAY_HAS_BEEN_PAST;
			LOGGER.info(DAY_HAS_BEEN_PAST);
			return "newVacationFailed";
		} else {
			LOGGER.info("start getNew vacation");
			vacationDto = new VacationBeanHelper().buildVacationDto(this);
			vacationService = getServiceContext().getVacationService();
			pastDayEx = null;
			vacationService.addNewVacation(vacationDto);
		}
		LOGGER.info("vacation has been got success");
		return "newVacationSuccess";
	}

	public String tiredMethod() {
		LOGGER.info("open div with new vacation form");
		iAmTired = I_AM_TIRED_MESSAGE;
		return "tiredTrue";
	}

	public String hideDiv() {
		LOGGER.info("hide  div with new vacation form");
		iAmTired = null;
		pastDayEx = null;
		return "hideDiv";
	}

	public List<VacationDto> fillTable() {

		if (vacationList == null) {
			LOGGER.info("start fill vacation table");
			vacationDto = new VacationBeanHelper().buildVacationDto(this);
			vacationService = getServiceContext().getVacationService();
			vacationList = vacationService.fillTable(vacationDto);
		}
		return vacationList;
	}

	public String delete() {
		List<VacationDto> basicList = fillTable();
		List<VacationDto> superList = new ArrayList<VacationDto>();
		superList.addAll(basicList);
		List<VacationDto> list = null;
		VacationDto deletedObject = new VacationDto();
		if (list == null) {
			list = fillTable();
		}
		System.out.println("list size: " + list.size());
		list.remove(wonderIndex);
		System.out.println(wonderIndex);
		System.out.println("size: " + list.size());
		
		superList.removeAll(list);
		System.out.println("Size of basic list after deleteALL"+ superList.size());
		deletedObject = superList.get(0);
		vacationService.deleteVacationRow(deletedObject);
		return "deleteVacation";
	}

	public int getWonderIndex() {
		return wonderIndex;
	}

	public void setWonderIndex(int wonderIndex) {
		this.wonderIndex = wonderIndex;
	}

	public String getPastDayEx() {
		return pastDayEx;
	}

	public void setPastDayEx(String pastDayEx) {
		this.pastDayEx = pastDayEx;
	}

	public void setStartDate(Date startDate) {
		vacationDto.setStartDate(startDate);
	}

	public Date getStartDate() {
		return vacationDto.getStartDate();
	}

	public void setEndDate(Date endDate) {
		vacationDto.setEndDate(endDate);
	}

	public Date getEndDate() {
		return vacationDto.getEndDate();
	}

	public String getiAmTired() {
		return iAmTired;
	}

	public void setiAmTired(String iAmTired) {
		this.iAmTired = iAmTired;
	}

	public void setDescription(String description) {
		vacationDto.setDescription(description);
	}

	public String getDescription() {
		return vacationDto.getDescription();
	}

	public boolean getTableState() {
		return vacationDto.getState();
	}

	public void setTableState(boolean state) {
		vacationDto.setState(state);
	}

}
